package com.example.leoss.duanxingxufa;

import android.Manifest;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.example.leoss.R;
import com.example.leoss.zhuye.File_xianshi.ToFile_duanxinactivity;


public class MsgMobanActivity extends AppCompatActivity implements View.OnClickListener {
    EditText id_msgmoban_ed_model1s;
    Button id_msgmoban_bt_phone1s,id_msgmoban_bt_duanxinym1;

    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS};
    private static ProgressDialog dialog;
    Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_msg_moban);
        initView();
        setTitle("短信模板");
        quanxian();
        verifyStoragePermissions(this);
    }

    private void initView()
    {
        id_msgmoban_ed_model1s = (EditText) findViewById(R.id.id_mesmoban_ed_model);
        id_msgmoban_bt_phone1s = (Button) findViewById(R.id.id_msgmoban_bt_phone);
        id_msgmoban_bt_duanxinym1=findViewById(R.id.id_msgmoban_bt_duanxinym);
        id_msgmoban_bt_duanxinym1.setOnClickListener(this);
        id_msgmoban_bt_phone1s.setOnClickListener(this);
    }

    private void quanxian(){
        String[]quan={Manifest.permission.SEND_SMS,Manifest.permission.READ_SMS,
                Manifest.permission.RECEIVE_SMS, Manifest.permission.RECEIVE_MMS,
                Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS,};
        for (String per:quan){
            if (ContextCompat.checkSelfPermission(this,per)!= PackageManager.PERMISSION_GRANTED){
                ActivityCompat.requestPermissions(this,quan,10000);
            }
        }
    }

    @Override
    public void onClick(View view)
    {
        switch (view.getId())
        {
            case R.id.id_msgmoban_bt_phone:
                 intent = new Intent();
                intent.putExtra("smsModel",id_msgmoban_ed_model1s.getText().toString());//EdiText选择的模板


                Handler handler=new Handler();
                Runnable runnable=new Runnable()
                {
                    @Override
                    public void run()
                    {

                        intent.setClass(MsgMobanActivity.this, ToFile_duanxinactivity.class);
                        intent.putExtra("data",1);
                        startActivityForResult(intent, 3);
                    }
                };
                handler.post(runnable);
                dialog=ProgressDialog.show(MsgMobanActivity.this,"请稍等","正在打开本地Excel",true,true);
                break;

            case R.id.id_msgmoban_bt_duanxinym:
                startActivity(new Intent(MsgMobanActivity.this,SendActivity.class));break;
        }
    }

    public static void closeProgressDialog() {
        dialog.dismiss();
    }

    public void verifyStoragePermissions(Activity activity)
    {
        int permission = ActivityCompat.checkSelfPermission(activity,
                Manifest.permission.ACCESS_FINE_LOCATION);

        if (permission != PackageManager.PERMISSION_GRANTED)
        {
            ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,
                    REQUEST_EXTERNAL_STORAGE);
        }


    }

}
